package com.rc.saas.mini.service.msg;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.mini.mapper.msg.TenantCustomMsgMapper;
import com.rc.saas.mini.model.member.MemberInfo;
import com.rc.saas.mini.model.msg.TenantCustomMsg;
import com.rc.saas.mini.model.msg.TenantCustomMsgExample;
import com.rc.saas.mini.service.member.MemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TenantCustomMsgServiceImpl implements  TenantCustomMsgService {
    @Autowired
    private TenantCustomMsgMapper tenantCustomMsgMapper;

    @Autowired
    private MemberInfoService memberInfoService;

    @Override
    public TenantCustomMsgMapper getMapper() {
        return tenantCustomMsgMapper;
    }

    @Override
    public PageInfo<TenantCustomMsg> listPageTenantCustomMsg(String tenantCode, Long memberId,  Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        TenantCustomMsgExample example = new TenantCustomMsgExample();
        example.createCriteria().andMemberIdEqualTo(memberId).andTenantCodeEqualTo(tenantCode);
        example.setOrderByClause(" custom_msg_id  desc");
        List<TenantCustomMsg> tenantCustomMsgs = tenantCustomMsgMapper.selectByExample(example);
        return new PageInfo <TenantCustomMsg>(tenantCustomMsgs);
    }

    @Override
    public void insertCustomMsg(Long memberId, String tenantCode, String msg) {
        //更新member表 last_custom_msg_time
        MemberInfo memberVo = new MemberInfo();
        memberVo.setMemberId(memberId);
        memberVo.setTenantCode(tenantCode);
        memberVo.setLastCustomMsgTime(new Date());
        memberInfoService.updateByPrimaryKeySelective(memberVo);

        TenantCustomMsg tenantCustomMsg = new TenantCustomMsg();
        tenantCustomMsg.setTenantCode(tenantCode);
        tenantCustomMsg.setMemberId(memberId);
        tenantCustomMsg.setSendTime(new Date());
        tenantCustomMsg.setMsgType("0");
        tenantCustomMsg.setMsgSource("0");
        tenantCustomMsg.setContent(msg);
        tenantCustomMsg.setIsRead("0");
        tenantCustomMsg.setCreatedBy(String.valueOf(memberId));
        tenantCustomMsg.setCreatedTime(new Date());
        tenantCustomMsg.setLastUpdatedBy(String.valueOf(memberId));
        tenantCustomMsg.setLastUpdatedTime(new Date());
        tenantCustomMsgMapper.insertSelective(tenantCustomMsg);
    }

}
